package easyexcel.listener;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.Map;

/**
 * describe:
 *
 * @author huxingjie
 * @date 2019/09/11
 * @since 4.0
 */
@Slf4j
public class CustomStringStringConverter implements Converter<String> {
    Map<String, String> map = Maps.newHashMap();

    Map<String, String> excelGB1400ViperVo_isOnline = Maps.newHashMap();

    public CustomStringStringConverter() {
        map.put("标清", "SD");
        map.put("高清", "HD");
        map.put("离线", "OFF");
        map.put("在线", "ON");
        excelGB1400ViperVo_isOnline.put("在线", "1");
        excelGB1400ViperVo_isOnline.put("离线", "2");
        excelGB1400ViperVo_isOnline.put("其他", "9");
    }

    @Override
    public Class supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * 这里读的时候会调用 {\"TCP\":\"0\",\"UDP\":\"1\"}"
     *
     * @param cellData            NotNull
     * @param contentProperty     Nullable
     * @param globalConfiguration NotNull
     * @return
     */
    @Override
    public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
                                    GlobalConfiguration globalConfiguration) {
        String value = cellData.getStringValue();
        if (StringUtils.isNotBlank(value) && map.containsKey(value)) {
            return map.get(value);
        } else {
            log.error("setCellValueToBeanProperty setField exception, field:" + contentProperty.getHead().getFieldName() + ", object:");
            return null;
        }
    }

    /**
     * 这里是写的时候会调用 不用管
     *
     * @param value               NotNull
     * @param contentProperty     Nullable
     * @param globalConfiguration NotNull
     * @return
     */
    @Override
    public CellData convertToExcelData(String value, ExcelContentProperty contentProperty,
                                       GlobalConfiguration globalConfiguration) {
        return new CellData(value);
    }
}

    